*Title: NSX Reproduce 1.0.do
*Authors: Jack Reilly, Jack Belk
*Date: 10.4.22
*Purpose: Reproduce Statistics & Figures in Reilly & Belk, "Political And Social Discussion Network Survey Items are Not Interchangeable" (JEPS)
*Requires: 2020 CES - NCF Module Subset
*Output: figures (to working directory)
*Platform: Stata 17 on macOS 12.6, arm/M1 architecture

*Identify file paths
*The script expects the data file, "CCES20_NCF_OUTPUT.sav", to be in this directory, and will output graphics here as well
cd "~/Desktop/working/NSX/reproduce"
import spss using "CCES20_NCF_OUTPUT_vv.sav", clear
keep teamweight NCF710A NCF710B newsint

*Final graphics in this paper utilize two nonstandard Stata packages.
*First, the mplotoffset package, which can be found and installed here (uncomment to run):
*findit mplotoffset

*Second, the "modern" scheme, which can be downloaded here (uncomment to run):
*net install scheme-modern, from("https://raw.githubusercontent.com/mdroste/stata-scheme-modern/master/")

******************
*Preparatory Work*
******************

*Make variables lowercase, add numbers to labels
rename *, lower
numlabel, add

*weights
gen w=teamweight

*Generate new network size variables where variable values match reported network size
gen netpol=ncf710a-1
gen netimp=ncf710b-1

*Generate full netsize variable along with condition indicator and variable labels
gen netsize=.

replace netsize=netpol if netpol!=.
replace netsize=netimp if netimp!=.

recode netpol .=. *=1, gen(netpcondition)
recode netimp .=. *=1, gen(neticondition)

gen condition_pol=.
replace condition = 1 if netpc==1
replace condition = 0 if netic==1

label define exp_cond 0 "Social" 1 "Politics"
label values condition_p exp_cond

**********************
*MAIN PAPER MATERIALS*
**********************

*Summary Statistics
sum netpol netimp, detail

**********************************
*TABLE 1: Sample Difference Tests*
**********************************

ttest netsize, by(condition)
ksmirnov netsize, by(condition) exact
*exact p-value reported in paper

**********************************
*FIGURE 1: Network Size Histogram*
**********************************

*Generate offset for overlapping histogram
gen netpolhist=netpol+.1
gen netimphist=netimp-.1

*Initial Graph
twoway (hist netimphist, discrete freq fcolor(green%50) lcolor(none) gap(50) barwidth(.9)) ///   
 (hist netpolhist, discrete freq fcolor(purple%70) lcolor(none) gap(50) barwidth(.9))

*Customized labels & scheme
twoway (hist netimphist, discrete freq fcolor(green%50) lcolor(none) gap(50) barwidth(.9)) ///   
 (hist netpolhist, discrete freq fcolor(purple%70) lcolor(none) gap(50) barwidth(.9)), ///        
       legend(region(lwidth(thin) lpattern(solid)) title(Condition, size(small)) order(1 "Social" 2 "Politics" )) scheme(modern) xtitle(Reported Network Size) plotregion(lcolor(black)) xlabel(0 "0" 5 "5" 10 "10" 11 "10+")

graph export "figure1histogram.pdf", replace

**********************************
*FIGURE 2: Predicted Network Size*
**********************************

*Recode and label political interest
*New variable: lower numbers==lower interest; higher numubers==higher interest
recode newsint 7=. 1=4 2=3 3=2 4=1, gen(interest)
label define newinterest 1 "Hardly at all" 2 "Only now and then" 3 "Some of the time" 4 "Most of the time"
numlabel newinterest, add
label values interest newinterest

*Model
*Censored count data - poisson interaction
cpoisson netsize i.condition##i.interest [pw=w], ul(11)

*Calculate Marginal Predicted Values
margins interest#condition

*Base Plot
marginsplot, recast(scatter) plot1opts(color(green)) plot2opts(symbol(triangle) color(purple)) ci1opt(color(green)) ci2opt(color(purple))

*Final Plot
mplotoffset, title("") offset(0.1) scheme(modern) recast(scatter) ytit(Predicted Network Size) xlab(1 "Hardly at all" 2 "Only now and then" 3 "Some of the time" 4 "Most of the time") xtit(Political Interest (Follow Public Affairs))  legend(region(lwidth(thin) lpattern(solid)) title(Condition, size(small))) plotregion(lcolor(black)) scale(1.2) plot1opts(color(green)) plot2opts(symbol(triangle) color(purple)) ci1opt(color(green)) ci2opt(color(purple))

graph export "figure2predicted.pdf", replace


********************
*APPENDIX MATERIALS*
********************

****************************************************************
*TABLE A1: Multinomial Logit: Netsize by Experimental Condition*
****************************************************************

*Multinomial Logistic Regression
mlogit netsize i.condition

***********************************
*FIGURE A1: Marignal Probabilities*
***********************************

*Calculate Marginal Effects
margins, dydx(condition)

*Base plot
marginsplot, recast(scatter)

*Final schemed and styled plot
marginsplot, scheme(modern) recast(scatter)  xlabel(0 "0" 1 "1" 2 "2" 3 "3" 4 "4" 5 "5" 6 "6" 7 "7" 8 "8" 9 "9" 10 "10" 11 "10+") title("") yline(0, lp(dash) lc(maroon)) xtit(Network Size) ytit("Marginal Probability" " ") plotregion(lcolor(black)) yscale(range(-.115 .115))

graph export "figureA1mlogit.pdf", replace


****************************
*TABLE A2: Censored Poisson*
****************************

*Censored count data - poisson interaction
cpoisson netsize i.condition##i.interest [pw=w], ul(11)

*****************************
*FIGURE A2: Marginal Effects*
*****************************

*Calculate marginal effects
margins, dydx(condition) by(interest)

*Draw marginal effects plot (base)
marginsplot

*Draw marginal effects plot (styled)
marginsplot, scheme(modern) title("") xtit(Political Interest (Follow Public Affairs)) ytit(Effect on Predicted Network Size) plotregion(lcolor(black)) xlab(1 "Hardly at all" 2 "Only now and then" 3 "Some of the time" 4 "Most of the time") xscale(range(0.75 4.25)) yline(0, lp(dash) lc(maroon))

graph export "figureA2cpmarginal.pdf", replace
